var htmlstr;
var code;
var gncdList;
var searchkey;
var searchstr;
var flag = false;
$(document).ready(function () {
    querySubMenu();
    setTimeout("closeDiv()",2000);
});

function second_onclick(param, obj) {
    /*  $(obj).siblings().css("background-color","#FFF");*/
    $(obj).siblings().find("li a").css("background-color", "#FFF");
    $(".secondIconA").show();
    $(".secondIconB").hide();
    $("#" + param + "A").hide();
    $("#" + param + "B").show();
    if (!$('body').hasClass('mini-navbar'))
        $("body").removeClass("mini-navbar");
    else {
        $("body").toggleClass("mini-navbar");
        SmoothlyMenu();
    }
}

/**
 * 一级菜单鼠标移入菜单操作
 */
function menuMousemoveF(obj) {
    $(obj).children(".firstIconB").show();
    $(obj).children(".firstIconA").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMousemove(obj) {
    $(obj).children(".secondIconB").show();
    $(obj).children(".secondIconA").hide();
    if( $("body").hasClass("mini-navbar")) {
        $(obj).children(".secondIconA").siblings("span").show();
    }
}

/**
 * 一级菜单鼠标移出菜单操作
 */
function menuMouseoutF(obj) {
    $(obj).children(".firstIconA").show();
    $(obj).children(".firstIconB").hide();
}

/**
 * 二级菜单鼠标移入菜单操作
 */
function menuMouseout(obj) {
    $(obj).children(".secondIconB").hide();
    $(obj).children(".secondIconA").show();
    if( $("body").hasClass("mini-navbar")) {
        $(obj).children(".secondIconA").siblings("span").hide();
    }
}

//查询二三级菜单
function querySubMenu() {
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        url: "index/getGncdTree?applicationId=DQMT",
        success: function (jsonData) {
            if(jsonData.code == 200){
                createMenu(jsonData.tree);
                gncdList = jsonData.tree;
                localStorage.setItem("gncdList",JSON.stringify(jsonData.tree));
            }
        },
        error: function (errorMsg) {
        }
    });
}

function createMenu(secondlist){
    var menuHtml = '';
    $.each(secondlist, function (index, entity) {
        var secondHtml = '';
        if (entity.thirdlist && entity.thirdlist.length >0) {
            secondHtml += '<li  style="margin:2px 0"  onclick="second_onclick(' + entity.mcode + ',this)">';
            secondHtml += '<a class="second-a J_menuItem" href="" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
            secondHtml += '<img class="secondIconA" id="' + entity.mcode + 'A" src="img/icon/' + entity.mcode + 'A.png" style="margin-right:5px;margin-bottom: 4px;display: inline"></img>';
            secondHtml += '<img class="secondIconB" id="' + entity.mcode + 'B" src="img/icon/' + entity.mcode + 'B.png" style="display:none;margin-right:5px;margin-bottom: 4px"></img>';
            secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
            // secondHtml += '<span class="fa arrow"></span>';
            secondHtml += '</a>';
            if (null != entity.thirdlist) {
                var thirdHtml = '';
                if (entity.thirdlist.length > 0) {
                    secondHtml += '<ul class="nav-second-wn nav-second-level-wn">';
                    $.each(entity.thirdlist, function (index, thirdEntity) {
                        thirdHtml += '<li>';
                        //   thirdHtml += '<a class="J_menuItem" href=' + thirdEntity.module + '?mcode=' + thirdEntity.mcode + '&cdType=third>' + thirdEntity.mcaption + '</a>';
                        thirdHtml += '<a class="J_menuItem" href="" >' + thirdEntity.mcaption + '</a>';
                        thirdHtml += '<ul class="nav-second-wn nav-second-level-wn">';
                        thirdHtml += '<li style="padding-left: 20px;"><a id="'+thirdEntity.mcode+'" class="J_menuItem" href="'+ thirdEntity.mcode +'">数据说明</a></li>';
                        thirdHtml += '</ul>';
                        thirdHtml += '</li>';
                    });
                    secondHtml += thirdHtml;
                    secondHtml += '</ul>';
                }
            }
            secondHtml += '</li>';
        } else {
            secondHtml += '<li onclick="second_onclick(' + entity.mcode + ',this)">';
            //   secondHtml += '<a class="J_menuItem" href="'+ entity.module +'" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
            secondHtml += '<a class="J_menuItem" href="" onmousemove="menuMousemove(this)" onmouseout="menuMouseout(this)">';
            secondHtml += '<img class="secondIconA" id="' + entity.mcode + 'A" src="img/icon/' + entity.mcode + 'A.png" style="margin-right:5px;margin-bottom: 4px;display: inline"></img>';
            secondHtml += '<img class="secondIconB" id="' + entity.mcode + 'B" src="img/icon/' + entity.mcode + 'B.png" style="display:none;margin-right:5px;margin-bottom: 4px"></img>';
            secondHtml += '<span class="nav-label">' + entity.mcaption + '</span>';
            secondHtml += '</a>';
            secondHtml += '<ul class="nav-second-wn">';
            secondHtml += '<li style="padding-left: 20px;"><a id="'+entity.mcode+'" class="J_menuItem" href="'+ entity.mcode +'">数据说明</a></li>';
            secondHtml += '</ul>';
            secondHtml += '</li>';
        }
        menuHtml += secondHtml;
    });
    $('#side-menu').html(menuHtml);
    $("#side-menu").metisMenu();
    $(".J_menuItem").on("click", c);
}

function c() {
    code = '';
    var o = $(this).attr("href"),
        m = $(this).data("index"),
        l = $.trim($(this).text()),
        k = true;
    if (o == undefined || $.trim(o).length == 0) {
        return false;
    }
    $(".nav-second-wn").find("li").removeClass("active");
    var a_id = '#' + o;
    $(a_id).parent().addClass("active");
    $(a_id).parent().parent().attr("aria-expanded",'true');
    $(a_id).parent().parent().addClass("collapse in");
    $(a_id).parent().parent().css("height","auto");
    $(a_id).parent().parent().parent().parent().attr("aria-expanded",'true');
    $(a_id).parent().parent().parent().parent().addClass("collapse in");
    $(a_id).parent().parent().parent().parent().css("height","auto");
    code = o;
    if(flag){
        flag = true;
    }else {
        flag = false;
    }
    queryHelpHtml();
    return false;
}

//添加帮助文档
function addHelpHtml() {
    var html ={};
    html.mcode = code;
    htmlstr = editor.txt.html();
    $.each(gncdList, function (index, entity) {
        if (entity.thirdlist && entity.thirdlist.length >0) {
            $.each(entity.thirdlist, function (index, thirdEntity) {
                if (code == thirdEntity.mcode){
                    html.caption = thirdEntity.mcaption;
                    html.fcode = thirdEntity.fcode;
                    html.pcode = thirdEntity.pcode;
                }
            });
        }else{
            if (code == entity.mcode){
                html.caption = entity.mcaption;
                html.fcode = entity.fcode;
                html.pcode = entity.pcode;
            }
        }
    });
    var data={
        mcode: html.mcode,
        mcpation: html.caption,
        fcode: html.fcode,
        pcode: html.pcode,
        content: htmlstr
    };
    data = JSON.stringify(data);
    if(code == null || code == ''){
        alert('请先选中左侧功能菜单');
    }else{
        $.ajax({
            type: "POST",
            contentType: 'application/json',
            dataType: "json",
            url: 'index/addHelpHtml',
            data: data,
            success: function (jsonData) {
                if(jsonData.code == 200){
                    alert("添加成功");
                }else{
                    alert(jsonData.msg);
                }
                queryHelpHtml();
            },
            error: function (errorMsg) {
            }
        });
    }
}

//查询帮助文档内容
function queryHelpHtml() {
  //  openDiv();
    document.getElementById("div_content").innerHTML= "加载数据中，请稍后......";
    var data = {
      mcode: code,
      searchKey: searchkey,
    };
    data = JSON.stringify(data);
    $.ajax({
        type: "POST",
        contentType: 'application/json',
        dataType: "json",
        url: "index/getHelpHtml",
        data: data,
        success: function (jsonData) {
            if(jsonData.code == 200){
                if(jsonData.data.length == 1){
                    htmlstr = jsonData.data[0].content;
                    if(flag){
                        htmlstr = htmlstr.replace(searchstr, '<a id="flag" style="color: black;background-color:rgb(249, 150, 59);;pointer-events:none;">' + searchstr + '</a>');
                    }
                    document.getElementById("div_content").innerHTML= htmlstr;
                    if(flag){
                        window.location.href="#flag";
                        flag = false;
                    }
                }else if(jsonData.data.length > 1){
                    var html_content = '';
                    html_content += '<ul style="font-size: 17px">';
                    $.each(jsonData.data, function (index, entity) {
                        var content = entity.content.replaceAll(/<[/\w\s]*.*?>/g,"");
                        html_content += '<li>';
                        html_content += '<a class="J_menuItem" href="'+ entity.mcode +'">'+ entity.mcaption +'</a><br>';
                        var str = content.substring(content.indexOf(searchstr)-15, content.indexOf(searchstr)+15+searchstr.length);
                        str = str.replace(searchstr,'<span style="background-color: rgb(249, 150, 59);">'+searchstr+'</span>');
                        html_content += '<span>...'+ str +'...</span>';
                        html_content += '</li><br>';
                    });
                    html_content += '</ul>';
                    document.getElementById("div_content").innerHTML= html_content;
                    flag = true;
                    $(".J_menuItem").on("click", c);
                }else {
                    htmlstr = '暂无数据';
                    document.getElementById("div_content").innerHTML= htmlstr;
                }
            }
         //   closeDiv();
        },
        error: function (errorMsg) {
        }
    });
}

//更新帮助文档内容
function updateHelpHtml() {
    htmlstr = editor.txt.html();
    var data = {
        mcode: code,
        content: htmlstr,
    };
    data = JSON.stringify(data);
    if(code == null || code == ''){
        alert('请先选中左侧功能菜单');
        return;
    }
    $.ajax({
        type: "POST",
        contentType: 'application/json',
        dataType: "json",
        url: "index/updateHelpHtml",
        data: data,
        success: function (jsonData) {
            if(jsonData.code == 200){
                alert('更新成功');
            }
            queryHelpHtml();
        },
        error: function (errorMsg) {
        }
    });
}

function confirm() {
    if(htmlstr == null || htmlstr == '' || htmlstr == '暂无数据'){
        addHelpHtml();
    }else{
        updateHelpHtml();
    }
}

function getHtml() {
    htmlstr = editor.txt.html();
    document.getElementById("div_content").innerHTML= htmlstr;
}

function clearAll() {
    editor.txt.clear();
}

function setHtmltoEditor() {
    editor.txt.html(htmlstr);
}

function searchByKey() {
    searchkey = document.getElementById("input").value;
    searchstr = searchkey;
    code = '';
    queryHelpHtml();
    searchkey = '';
}
function closeDiv() {
    document.getElementById('loading').style.visibility='hidden';
}
function openDiv() {
    document.getElementById('loading').style.visibility='visible';
}